package com.sky.controller.admin;

import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("adminOrderController")
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired private OrderService orderService;


    // 订单搜索
    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("条件搜索订单：{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.historyOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }
    // 订单详情
    @GetMapping("/details/{id}")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("查询订单详情：{}", id);
        OrderVO orderVO = orderService.orderDetail(id);
        return Result.success(orderVO);
    }
    // 订单统计
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics() {
        log.info("订单统计");
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }
    //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
    // 订单接单
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("订单确认：{}", ordersConfirmDTO);
        ordersConfirmDTO.setStatus(Orders.CONFIRMED);
        orderService.updatezt(ordersConfirmDTO);
        return Result.success();
    }
    // 派送
    @PutMapping("delivery/{id}")
    public Result delivery(@PathVariable Long id) {
        OrdersConfirmDTO ordersConfirmDTO= new OrdersConfirmDTO();
        ordersConfirmDTO.setId(id);
        ordersConfirmDTO.setStatus(Orders.DELIVERY_IN_PROGRESS);
        log.info("订单派送：{}", ordersConfirmDTO);
        orderService.updatezt(ordersConfirmDTO);
        return Result.success();
    }
    // 完成
    @PutMapping("complete/{id}")
    public Result complete(@PathVariable Long id) {
        OrdersConfirmDTO ordersConfirmDTO= new OrdersConfirmDTO();
        ordersConfirmDTO.setId(id);
        ordersConfirmDTO.setStatus(Orders.COMPLETED);
        log.info("订单完成：{}", ordersConfirmDTO);
        orderService.updatezt(ordersConfirmDTO);
        return Result.success();
    }
    // 拒单
    @PutMapping("/rejection")
    public Result rejection(@RequestBody Orders orders) {
        log.info("订单拒单：{}", orders);
        orders.setStatus(Orders.CANCELLED);
        orderService.update(orders);
        return Result.success();
    }
    // 取消订单
    @PutMapping("/cancel")
    public Result cancel(@RequestBody Orders orders) {
        log.info("取消订单：{}", orders);
        orders.setStatus(Orders.CANCELLED);
        orderService.update(orders);
        return Result.success();
    }


}
